Skip to content

fix: use QJsonObject for event logger messages#187

Merged
Ivy233 merged 1 commit intolinuxdeepin:masterfrom
Ivy233:feat/event-logger-multifield
Apr 29, 2026
Merged

fix: use QJsonObject for event logger messages#187
Ivy233 merged 1 commit intolinuxdeepin:masterfrom
Ivy233:feat/event-logger-multifield

Conversation

@Ivy233
Copy link
Copy Markdown
Contributor

@Ivy233 Ivy233 commented Apr 26, 2026

Use QJsonObject directly for EventLoggerData messages so JSON payloads are preserved when writing event logs. Remove the string key/value helper that no longer matches the object-based message payload.

使用 QJsonObject 直接承载 EventLoggerData 的 message 字段,确保写入事件日志时保留 JSON 载荷内容。移除不再匹配对象化 message 载荷的字符串键值辅助接口。

Summary by Sourcery

Switch event logger message payloads to use QJsonObject instead of string maps to preserve structured JSON data.

Bug Fixes:

  • Ensure event logger messages retain full JSON payloads by storing them as QJsonObject rather than flattening to string key/value pairs.

Enhancements:

  • Remove the string-based writeEventLog helper and simplify JSON serialization by writing the QJsonObject message directly into the event log output.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Apr 26, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Refactors EventLoggerData to store event messages as a QJsonObject instead of a string map so structured JSON payloads are preserved, and simplifies EventLogger by removing the now-mismatched string key/value helper and serializing the stored QJsonObject directly.

Class diagram for updated EventLoggerData and EventLogger

classDiagram
class EventLoggerData {
  qint64 tid
  QString target
  QJsonObject message
  EventLoggerData()
  EventLoggerData(qint64 tid, const QString &target, const QJsonObject &message)
}

class EventLogger {
  void writeEventLog(const EventLoggerData &data)
  bool init(QString package_id, bool enable_sig)
  QJsonDocument toJson(EventLoggerData data)
}

EventLogger --> EventLoggerData : uses
Loading

File-Level Changes

Change Details Files
Change EventLoggerData message storage from a string map to QJsonObject and propagate this through constructors and JSON serialization.
  • Replace the message field type from QMap<QString, QString> to QJsonObject in EventLoggerData.
  • Update the default constructor to initialize message with an empty QJsonObject instead of an empty QMap.
  • Update the value constructor to take a const QJsonObject & for message instead of a QMap<QString, QString>.
  • Update makeJsonDocument to assign data.message directly into the "message" JSON field instead of constructing it from a key/value iterator.
cpp-include/eventlogger.hpp
Remove the convenience overload that wrote event logs from a single key/value string pair, which no longer fits the object-based message payload.
  • Delete writeEventLog overload taking tid, target, key, and value and delegating to EventLoggerData with a single-entry map.
cpp-include/eventlogger.hpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • If simple key/value logging is still a common case, consider keeping a convenience writeEventLog(tid, target, key, value) that internally constructs a QJsonObject so existing call sites don’t all have to manually build JSON objects.
  • Since EventLoggerData::message changed type, double-check any implicit uses (e.g., brace-initialization with { {"key", "value"} }) still behave as intended with QJsonObject, or consider adding explicit factory/helpers to make the new usage pattern clearer.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- If simple key/value logging is still a common case, consider keeping a convenience `writeEventLog(tid, target, key, value)` that internally constructs a `QJsonObject` so existing call sites don’t all have to manually build JSON objects.
- Since `EventLoggerData::message` changed type, double-check any implicit uses (e.g., brace-initialization with `{ {"key", "value"} }`) still behave as intended with `QJsonObject`, or consider adding explicit factory/helpers to make the new usage pattern clearer.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@Ivy233 Ivy233 force-pushed the feat/event-logger-multifield branch 2 times, most recently from 402e4c9 to 4eed94c Compare April 29, 2026 10:21
为 EventLogger 添加 cmake find_package 支持和自动初始化

- Add DDEAPIConfig.cmake for find_package(DdeApi)
- Auto-init with DSGApplication::id() when not explicitly initialized
- Use QJsonObject for structured event log messages
- Refactor init logic into doInit() method
- Install cmake config to /usr/share/cmake/DDEAPI/

PMS: TASK-388657
@Ivy233 Ivy233 force-pushed the feat/event-logger-multifield branch from 4eed94c to 208c3fc Compare April 29, 2026 13:09
@deepin-ci-robot
Copy link
Copy Markdown

deepin pr auto review

这份代码变更主要涉及了三个部分:CMake配置文件的添加、Makefile的修改以及C++头文件的重构。我将从语法逻辑、代码质量、代码性能和代码安全四个方面进行审查。

1. 语法逻辑

CMake配置文件 (cmake/DDEAPI/DDEAPIConfig.cmake):

  • 问题: set(DDE_API_INCLUDE_DIRS "/usr/include") 硬编码了路径。
    • 影响: 这使得库无法安装到非标准前缀(如 /usr/local)下被正确找到。
    • 建议: 使用 CMAKE_INSTALL_PREFIX 或相对路径,或者使用 get_filename_component 获取配置文件所在位置来推断包含目录。例如:
      get_filename_component(_DDEAPI_INSTALL_PREFIX "${CMAKE_CURRENT_LIST_DIR}/../../../" ABSOLUTE)
      set(DDE_API_INCLUDE_DIRS "${_DDEAPI_INSTALL_PREFIX}/include")

Makefile:

  • 问题: cp -R 命令用于复制单个文件。
    • 影响: 虽然功能上可行,但 -R 通常用于递归复制目录,对于单个文件使用 cpinstall 更规范。
    • 建议: 使用 install 命令替代 cp,因为 install 可以更好地处理权限问题,且是构建安装的标准工具。
      install -D -m 644 cmake/DDEAPI/DDEAPIConfig.cmake ${DESTDIR}${PREFIX}/share/cmake/DDEAPI/DDEAPIConfig.cmake
    • 问题: mkdir -pv 创建目录。
    • 建议: 如果使用 install -D,它会自动创建父目录,无需显式 mkdir

C++头文件 (eventlogger.hpp):

  • 问题: #include <DSGApplication> 被添加,但未在 diff 中显示其是否在项目中可用。
    • 建议: 确保此头文件在所有依赖此头文件的项目中都可用,否则会导致编译失败。
  • 逻辑变更: writeEventLog 现在在内部调用 doInit,并传入 Dtk::Core::DSGApplication::id()
    • 问题: 这将初始化逻辑与 DSGApplication 强耦合。如果用户在没有 DSGApplication 实例的环境中使用此头文件(例如纯 Qt 应用或非 DTK 应用),可能会崩溃或行为异常。
    • 建议: 检查 DSGApplication 是否已实例化,或者提供回退机制。

2. 代码质量

C++头文件:

  • 改进: 将 QMap<QString, QString> 改为 QJsonObject 是一个很好的改进。这直接减少了数据转换步骤,使 structToJson 函数更简洁,逻辑更清晰。
  • 改进: 移除了 writeEventLog 的重载函数(接受 keyvalue 的版本),简化了 API。这减少了维护负担,但也可能增加了调用者的代码量。需权衡易用性与接口简洁性。
  • 问题: 初始化逻辑从 init() 函数移到了 writeEventLog 内部。
    • 影响: 这是一种"惰性初始化"模式,优点是用户无需手动调用 init。缺点是初始化失败可能被静默忽略(仅打印 qDebug),且初始化时机不可控(发生在第一次写入时)。
    • 建议: 考虑保留显式的 init 接口供高级用户使用,或者确保惰性初始化的错误处理更加明显(例如通过返回值)。

3. 代码性能

  • 改进: 使用 QJsonObject 直接存储消息,避免了在 structToJson 中将 QMap 转换为 QJsonObject 的循环。这减少了 CPU 开销和临时对象的创建,提高了性能。
  • 问题: writeEventLog 中每次调用都会检查 m_initialized 并加锁。
    • 影响: 如果日志写入非常频繁,锁竞争可能成为瓶颈。
    • 建议: 考虑使用 std::call_once 或双重检查锁定(尽管 C++ 中实现正确较难)来优化初始化检查。不过,鉴于日志通常不是高频操作,当前实现可能已足够。

4. 代码安全

  • 问题: doInit 中调用 Dtk::Core::DSGApplication::id() 获取包名。
    • 风险: 如果 DSGApplication 未正确初始化,id() 可能返回空字符串或无效值,导致底层日志库行为异常。
    • 建议: 添加对 id() 返回值的检查,确保其非空。
  • 问题: dlsym 加载的函数指针(m_initialize, m_writeEventLog)在 doInit 中被检查是否为 nullptr
    • 风险: 如果动态库加载失败或符号缺失,日志功能将静默失效。
    • 建议: 考虑在构造函数或首次使用时更严格地检查这些指针,并在关键错误时抛出异常或终止程序(如果日志是关键功能)。
  • 问题: 移除了 DDE_EVENTLOG_DEBUG_ENABLE_CURRENT_VERSION 宏。
    • 影响: 这减少了调试灵活性,但提高了生产环境的安全性(防止意外启用调试日志)。这是一个合理的权衡。

总结与建议

  1. 修复 CMake 路径问题: 使用 CMAKE_INSTALL_PREFIX 或相对路径,避免硬编码 /usr/include
  2. 改进 Makefile: 使用 install 命令替代 cpmkdir,提高跨平台兼容性和可维护性。
  3. 解耦 DSGApplication: 确保在 DSGApplication 不可用时提供合理的回退或错误处理。
  4. 优化初始化逻辑: 考虑使用 std::call_once 优化初始化检查,并确保错误处理更加明显。
  5. 增强安全性: 检查 DSGApplication::id() 的返回值,并在关键错误时提供更明确的反馈。

这些改进将使代码更健壮、更易于维护,并在不同环境下表现更一致。

@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: 18202781743, Ivy233

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@Ivy233 Ivy233 merged commit b157a5a into linuxdeepin:master Apr 29, 2026
18 checks passed
@Ivy233 Ivy233 deleted the feat/event-logger-multifield branch April 29, 2026 13:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants